<template>
  <div id="app">
    <el-card shadow="always" class="el-card">
<!--      表单-->
      {{tbsearch}}
      <el-form :inline="true" :model="tbsearch"class="demo-form-inline">
          <el-form-item >
            <el-button  plain @click="add" >新建</el-button>
          </el-form-item>
          <el-form-item>
            <el-button   plain @click="de" >删除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button   plain @click="sx" >刷新</el-button>
          </el-form-item>
        <el-form-item label="规格名称" >
          <el-input v-model="tbsearch.specName" placeholder="规格名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button  plain @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    <el-table class="empty-table"
        :data="tbspecificationList"
        style="width: 100%">
      <el-table-column
          label="规格Id"
          align="center"
          prop="id">
      </el-table-column>
      <el-table-column
          label="规格名称"
          align="center"
          prop="specName">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="el-pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    </el-card>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{TbSpecification}}
      <el-form :model="TbSpecification">
        <el-form-item label="规格ID" :label-width="formLabelWidth">
          <el-input v-model="TbSpecification.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="TbSpecification.specName" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saves">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import tbSpecificationapi from "@/api/tbSpecification";


export default {
  data() {
    return {
      tbspecificationList: [],      // 当前页的所有学生信息集合
      page: 1,               // 当前页
      pageSize: 5,           // 每页大小
      total: 100,            // 总记录数
      searchtb:{},     // 要查找的学生信息对象
      tbsearch: {},    // 代表查询条件对象
      // classesList: [],
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      TbSpecification: {},              // 学生对象（添加或修改）
      title: '添加规格',
    }
  },

  created() {
    // this.findByPage(this.page,this.pageSize)
    this.search()
    // this.findTbs()


  },


  methods: {

    findTbs() {
      this.$axios.get('TbSpecification/list.do')
          .then(res => {
            console.log(res)
            // this.tbspecificationList=res.data;
            console.log("数据：", res.data)
            this.tbspecificationList = res.data
          })
    },

    // async findByPage() {
    //   // this.$axios.get(`/TbSpecification/findByPage.do?page=${this.page}&pageSize=${this.pageSize}`)
    //   //     .then(({data}) => {
    //   //       console.log("数据：", data)
    //   //       this.tbspecificationList =data.rows
    //   //       this.total = data.total
    //   //
    //   //     })
    //     const {data:res} = await tbSpecificationapi.findByPage(this.page,this.pageSize)
    //     this.tbspecificationList = res.rows
    //     this.total = res.total
    //
    // },

    async search(){
      const {data:res} = await tbSpecificationapi.search(this.page,this.pageSize,this.tbsearch)
      this.tbspecificationList = res.rows
      this.total = res.total
    },

    add(){
      this.dialogFormVisible = true;
    },

    async saves(){
      const {data:res} = await tbSpecificationapi.save(this.TbSpecification)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },


    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.search()
    }
  }
}
</script>

<style>
.el-card{
  width: 90%;

}
#app{
  display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
.demo-form-inline{
  text-align: right;
}




</style>
